﻿
//钩子函数
$(function () {
    getWarehouseData();
});

var obj = {
    warehouseId: '',
    warehouseNo: '',
    warehouseName: '',
    reservoirArea: '',
    locationCoding: '',
    warehouseArea: '',
    warehouseHead: '',
    warehouseState: '',
    warehouseRemark: '',
    createdBy: '',
    createdDate: '',
    updatedBy: '',
    updatedDate: '',
    deletedBy: '',
    deletedDate: '',
    isDeleted: ''
}

//返填
function getWarehouseData() {
    layui.use(function () {
        var form = layui.form;
        var warehouseId = localStorage.getItem("warehouseId");
        $.ajax({
            url: '/Warehouse/WarehouseBackfill',
            type: 'get',
            data: {
                warehouseId: warehouseId,
                token: localStorage.getItem("token")
            },
            success: function (res) {
                var res = JSON.parse(res)
                console.log(res)
                $.each(res, function () {
                    $("#warehouseNo").val(this.warehouseNo)
                    $("#warehouseName").val(this.warehouseName)
                    $("#reservoirArea").val(this.reservoirArea)
                    $("#locationCoding").val(this.locationCoding)
                    $("#warehouseArea").val(this.warehouseArea)
                    $("#warehouseHead").val(this.warehouseHead)
                    //状态
                    if (this.warehouseState == true) {
                        $('#normal').prop('checked', true)
                    } else {
                        $('#abnormal').prop('checked', true)
                    }
                    $("#warehouseRemark").val(this.warehouseRemark)
                    $("#createdBy").val(this.createdBy)
                    obj.warehouseId = this.warehouseId,
                        obj.warehouseNo = this.warehouseNo,
                        obj.warehouseName = this.warehouseName,
                        obj.reservoirArea = this.reservoirArea,
                        obj.locationCoding = this.locationCoding,
                        obj.warehouseArea = this.warehouseArea,
                        obj.warehouseHead = this.warehouseHead,
                        obj.warehouseState = this.warehouseState,
                        obj.warehouseRemark = this.warehouseRemark,
                        obj.createdBy = this.createdBy,
                        obj.createdDate = this.createdDate,
                        obj.updatedBy = this.updatedBy,
                        obj.updatedDate = this.updatedDate,
                        obj.deletedBy = this.deletedBy,
                        obj.deletedDate = this.deletedDate,
                        obj.isDeleted = this.isDeleted,
                        form.render(); // 重新渲染
                })
            },
        })
    })
}

layui.use(['layer', 'form', 'util'], function () {
    var layer = layui.layer;
    var $ = layui.jquery;
    var form = layui.form;
    var util = layui.util;

    form.on('submit(formDemo)', function (data) {
        var dto = data.field; // 获取表单字段值
        data.field.warehouseId = obj.warehouseId
        data.field.createdBy = obj.createdBy
        data.field.updatedBy = obj.updatedBy
        console.log(dto);
        $.ajax({
            url: '/Warehouse/UpdWarehouse',
            type: 'post',
            data: {
                dto: dto,
                token: localStorage.getItem("token")
            },
            success: function (res) {
                var res = JSON.parse(res)
                console.log(dto);
                console.log(res);
                if (res.code == 0) {
                    layer.alert("修改成功");
                    setTimeout(function () {
                        location.href = '/Warehouse/WarehouseShow'
                    }, 2000);
                } else {
                    layer.alert("修改失败");
                }
            }
        });

        return false; // 阻止默认 form 跳转
    });
    // 对弹层中的表单进行初始化渲染
    form.render();
});